// pages/daily/daily.js
const httpRequest = require('../../utils/http_utils.js')
const conn = require('../../utils/common.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '日常巡检',
    currentTab: 0,
    navScrollLeft: 0,
    navData: [{
        text: '设备间巡检'
      },
      {
        text: '设备巡检'
      }
    ],
    menulist: [],
    PageSize: 15, //单页数据条数
    CurrentPage: 1, //当前页码
    IsEquipRoom: 1, //是否设备间
    TaskName: '', //任务名称
    dailyList: [], //日常巡检集合
    totalNum: 0, //数据总条数
    showIs: false, //暂无图片
    downloading: false, //加载中
    noMore: false, //没有更多了
    loadingFailed: false, //数据加载失败
    Callback: "", //回调参数
    error: "", //失败提示
    info: '', //提示信息
    // code:0, //设备编码
    customHeight: 0, //自定义高度
  },

  tiaozhuan(e) { //页面跳转
    var rowguid = e.currentTarget.dataset.rowguid; //获取菜单rowguid
    var url = e.currentTarget.dataset.url; //获取按钮跳转地址
    console.log(url + "-------------扫一扫")
    if (url.indexOf('saoyisao') == -1) {
      wx.navigateTo({
        url: url + '?ParentGuid=' + rowguid,
      })
    } else {
      this.scancode();
    }
  },



  //扫一扫
  scancode: function() {
    var that = this
    wx.scanCode({
      success: function(res) {
        wx.navigateTo({
          url: '../Scan/Scan?code=' + res.result,
        })
      },
      fail: function(res) {
        console.log(JSON.stringify(res))
        // that.setData({
        //   error: '找不到照相机！'
        // })
      }
    })
  },

  

  switchNav(event) { //设备间/设备切换
    var cur = event.currentTarget.dataset.current;
    console.log(cur+"-----------点击切换")
    if (this.data.currentTab == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
    }
    if (cur == 0) {
      this.setData({
        PageSize: 15, //单页数据条数
        CurrentPage: 1, //当前页码
        IsEquipRoom: 1,
        dailyList: []
      })
      this.initData();
    } else if (cur == 1) {
      this.setData({
        PageSize: 15, //单页数据条数
        CurrentPage: 1, //当前页码
        IsEquipRoom: 2,
        dailyList: []
      })
      this.initData();
    }
  },

  initData: function() { //初始化数据
    var that = this;
    var CurrentParam = {
      PageSize: that.data.PageSize,
      CurrentPage: that.data.CurrentPage,
      IsEquipRoom: that.data.IsEquipRoom,
      TaskName: that.data.TaskName,
      RegionGuid: "",
      BuildGuid: "",
      FloorGuid: "",
      RoomGuid: ""
    }
    // console.log(JSON.stringify(param))
    httpRequest.PostValidateRequest(httpRequest.apiInfo.GetInspectTaskListMobileApi, CurrentParam,
      function(data) {
        console.log(JSON.stringify(data))
        var dailyArray = that.data.dailyList;
        if (data.pageData == null || data.pageData == [] || data.pageData == undefined || data.pageData == "") {
          console.log("fsadsaf")
          that.setData({
            showIs: true,
            noMore: false
          })
        } else {
          console.log(data.pageData.length + "_____________数据长度")

          for (var i = 0; i < data.pageData.length; i++) {
            var res = "/";
            try {
              res = "每" + conn.getDicText("巡检周期单位", data.pageData[i].InspectUnit) + data.pageData[i].InspectTime + "次" + "(第" + data.pageData[i].TaskIndex + "次巡检)";
            } catch (e) {
              res = "/";
            }
            var params = {
              TaskName: data.pageData[i].TaskName,
              Location: data.pageData[i].Location,
              IsAlarm: data.pageData[i].IsAlarm,
              TaskType: data.pageData[i].TaskType,
              RowGuid: data.pageData[i].RowGuid,
              RoomName: data.pageData[i].RoomName,
              ProjectEquipRoomGuid: data.pageData[i].ProjectEquipRoomGuid,
              cycle: res
            }
            dailyArray.push(params)
          }
          that.setData({
            dailyList: dailyArray,
            totalNum: data.totalNum,
            uploading: false,
            noMore: false,
            showIs: false
          })
        }

      },
      function(err) {

      })
  },

  //上拉加载
  scrollToLower: function() {
    var that = this
    console.log(that.data.totalNum + "_______总条数")
    var pages = Math.round((that.data.totalNum / 15) * 100) / 100
    if (that.data.CurrentPage > pages) {
      that.setData({
        noMore: true
      })
      setTimeout(() => {
        that.setData({
          noMore: false
        })
      }, 2000)
    } else {
      that.setData({
        noMore: false,
        downloading: true
      })
      setTimeout(() => {
        that.setData({
          downloading: false
        })
      }, 3000)
      that.setData({
        CurrentPage: that.data.CurrentPage + 1
      })
      that.initData();
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      customHeight: ((app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2) + 280
    })

    if (options.ParentGuid != undefined && options.ParentGuid != '') {
      var menu_list = this.data.menulist;
      var storage_list = wx.getStorageSync('UserInfo').AppLogin.AppModuleDTO.filter(function(item) {
        return item.ParentGuid === options.ParentGuid;
      });

      for (var i = 0; i < storage_list.length; i++) {
        var param = {
          RowGuid: storage_list[i].RowGuid,
          Name: storage_list[i].ModuleName,
          Icon: storage_list[i].BigIconAddress,
          ModuleUrl: storage_list[i].ModuleUrl
        }
        menu_list.push(param)
      }
      //根据RowGuid去重
      const RowGuid = 'RowGuid';
      menu_list = menu_list.reduce((all, next) => all.some((atom) => atom[RowGuid] == next[RowGuid]) ? all : [...all, next], []);
      this.setData({
        menulist: menu_list
      })
      console.log('menulist---', this.data.menulist)
    }
    this.initData();
    this.refreshView = this.selectComponent("#refreshView");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data.Callback + "---------------回调")
    //新增巡检记录后回调重新刷新列表
    if (this.data.Callback == '初始化') {
      this.setData({
        dailyList:[],
        PageSize: 15, //单页数据条数
        CurrentPage: 1, //当前页码
      })
      this.initData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  //触摸开始
  handletouchstart: function(event) {
    this.refreshView.handletouchstart(event)
  },
  //触摸移动
  handletouchmove: function(event) {
    this.refreshView.handletouchmove(event)
  },
  //触摸结束
  handletouchend: function(event) {
    this.refreshView.handletouchend(event)
  },
  //触摸取消
  handletouchcancel: function(event) {
    this.refreshView.handletouchcancel(event)
  },
  //页面滚动
  onPageScroll: function(event) {
    this.refreshView.onPageScroll(event)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    setTimeout(() => {
      console.log("scrollToUpper")
      var that = this
      that.setData({
        PageSize: 15, //单页数据条数
        CurrentPage: 1, //当前页码
        IsEquipRoom: that.data.IsEquipRoom, //是否设备间
        TaskName: '', //任务名称
        dailyList: [], //日常巡检集合
      })
      that.initData();
      this.refreshView.stopPullRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})